<?php

	require("includes/database.php");

	$inString = $_GET[q];
	
	list($codigo, $nombre) = split(" - ", $inString);

	$mySQL = "SELECT * FROM partes WHERE codigoTango='" . $codigo . "'";
	$result = readSQL($mySQL);
	if(count($result) != 0)
	{
		foreach($result as $value)
		{
			$nombreParte = $value['descripcionInterna'];
			$idParte = $value['idParte'];
		}
	}
	
?>
<table>
	<tr>
		<td><?php echo $nombreParte;?></td>
	</tr>
	<tr>
		<td>
			<?php
				//$mySQL = "SELECT S.*, A.nombre AS ALMACEN, SC.nombre AS SUCURSAL FROM stock S, almacenes A, sucursales SC WHERE S.idAlmacen = A.idAlmacen AND SC.idSucursal = A.idSucursal AND S.idParte = " . $idParte;
				$mySQL = "SELECT S.idStock, SUM(S.cantidad) AS cant, S.idAlmacen, T.idTipoUso, TU.nombre AS TIPOUSO, A.nombre AS ALMACEN, SC.nombre AS SUCURSAL FROM stock S join tiposusosxstock T ON S.idStock = T.idStock JOIN almacenes A ON A.idAlmacen = S.idAlmacen JOIN sucursales SC ON SC.idSucursal = A.idSucursal JOIN tiposusos TU ON TU.idTipoUso = T.idTipoUso WHERE S.idParte = $idParte AND S.cantidad > 0 GROUP BY T.idTipoUso, S.idAlmacen ORDER BY S.idAlmacen";
				$result = readSQL($mySQL);
				if(count($result) != 0)
				{
			?>
			<table cellpadding="3" cellspacing="3">
				<tr>
					<td>Almacen</td>
					<td>Cantidad</td>
					<td>Tipos de uso</td>
				</tr>
				<?php
					foreach($result as $value)
					{
						$nombreAlmacen = $value['SUCURSAL'] . " - " . $value['ALMACEN'];
						$cantidad = $value['cant'];
						$idStock = $value['idStock'];
						$tipoUso = $value['TIPOUSO'];
						echo "<tr>";
						echo "<td valign='top'>$nombreAlmacen</td>";
						echo "<td align='right' valign='top'>$cantidad</td>";
						echo "<td valign='top'>$tipoUso</td>";
						
						/*echo "<td><table cellpadding=0 cellspacing=0>";
						
						$mySQL = "SELECT TU.nombre FROM tiposusos TU, tiposusosxstock TUXS WHERE TUXS.idTipoUso = TU.idTipoUso AND TUXS.idStock=" . $idStock;
						$result = readSQL($mySQL);
						foreach($result as $value)
						{
							$tipoUso = $value['nombre'];
							echo "<tr>";
							echo "<td>$tipoUso</td>";
							echo "</tr>";
						}	
						
						echo "</table></td>";	*/					
						echo "<tr>";
					}
				}
				else
					echo "No hay informacion para dicha Parte.";
				?>
			</table>
		</td>
	</tr>
</table>